import { ref } from 'vue'
import { defineStore } from 'pinia'

// 检测结果类型定义
interface DetectionResult {
  type: string
  class?: string
  confidence: number
  bbox: [number, number, number, number]
}

// 前端使用的检测结果类型
interface FrontendDetectionResult {
  marked_image_url: string
  vehicle_marked_image_url?: string
  license_plate_text?: string
  confidence?: number | null
  model_runtime?: number | null
  vehicle_count?: number | null
  plates?: Array<{
    text: string
    confidence: number
  }> | null
  overall_accuracy: number | null
  detection_results?: DetectionResult[] | null
  model_name?: string
  detection_time?: string
  cropped_plate_url?: string
  ocr_plate_text?: string
  ocr_confidence?: number
  ocr_res_img_url?: string
  // 车辆-车牌联合检测结果字段
  plate_accuracy?: number
  vehicle_accuracy?: number
  plate_count?: number
  plate_detections?: Array<{
    bbox: [number, number, number, number]
    confidence: number
    ocr_confidence: number
    plate_text: string
    type: string
  }>
  vehicle_detections?: Array<{
    bbox: [number, number, number, number]
    class: string
    confidence: number
    type: string
  }>
}

export const useImageStore = defineStore('image', () => {
  const preprocessedImage = ref<string>('')
  const preprocessedImagePath = ref<string>('')
  const detectionResult = ref<FrontendDetectionResult | null>(null)

  function setPreprocessedImage(url: string, path?: string) {
    preprocessedImage.value = url
    if (path) {
      preprocessedImagePath.value = path
    }
  }

  function setDetectionResult(result: FrontendDetectionResult) {
    detectionResult.value = result
  }

  function resetImages() {
    preprocessedImage.value = ''
    preprocessedImagePath.value = ''
    detectionResult.value = null
  }

  return {
    preprocessedImage,
    preprocessedImagePath,
    detectionResult,
    setPreprocessedImage,
    setDetectionResult,
    resetImages
  }
}) 